package com.sso.manager;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sso.exception.DataBaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tools.util.StrUtils;
import com.sso.common.ConstantDate;
import com.sso.common.enums.FlagStatus;
import com.sso.dao.SysResourceRoleMapper;
import com.sso.entity.SysResourceRole;
import com.sso.exception.SelectException;

@Service
@Transactional(rollbackFor=Exception.class)
public class SysResourceRoleManager {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private SysResourceRoleMapper resourceRoleDao;

	public String save(SysResourceRole resourceRole) throws Exception {
		if(StrUtils.isEmpty(resourceRole.getId())){
			resourceRole.setId(ConstantDate.getUUID());
			resourceRoleDao.insert(resourceRole);
		}else{
			resourceRoleDao.update(resourceRole);
		}
		return FlagStatus.SUCCESS.value;
	}
	
	public List<SysResourceRole> selectByProperty(Map<String,Object> filterMap){
		return resourceRoleDao.selectByProperty(filterMap);
	}
	
	public SysResourceRole selectById(String id){
		SysResourceRole resourceRole = new SysResourceRole();
		Map<String,Object> filterMap = new HashMap<String, Object>();
		filterMap.put("id", id);
		List<SysResourceRole> list = this.selectByProperty(filterMap);
		if(list.size()>1){
			logger.info("查询数据量过大:list.size大小="+list.size());
		}else{
			resourceRole = list.get(0);
		}
		return resourceRole;
	}
	@Transactional(rollbackFor = Exception.class)
	public void deleteById(Map<String,Object> filterMap) throws Exception {
		resourceRoleDao.delete(filterMap);
	}
	
	public void deleteById(String id)throws Exception{
		try{
			Map<String,Object> filterMap = new HashMap<String, Object>();
			filterMap.put("id", id);
			resourceRoleDao.delete(id);
		}catch(RuntimeException e){
			e.printStackTrace();
			throw e;
		}
	}
	
	public List<SysResourceRole> selectByResourceId(String id)throws SelectException{
		Map<String,Object> filterMap = new HashMap<String, Object>();
		filterMap.put("resourceId", id);
		return this.selectByProperty(filterMap);
	}
}
